package top.goldenyear.porpoise.framework.web.core.generator.model;

import cn.hutool.v7.core.text.StrUtil;
import lombok.*;
import lombok.experimental.Accessors;

/**
 * 数据库表的列信息
 *
 * @author airhead
 */
@Data
@Accessors(chain = true)
public class ColumnMeta {
  /** 字段名称 */
  private String name;

  private String type;

  /** 字段注释 */
  private String comment;

  /** 是否为主键 */
  private Boolean primaryKey = false;

  /** 是否自增 */
  private Boolean autoIncrement = false;

  /** 是否可空 */
  private Boolean nullable = true;

  /** 属性名称 */
  private String property;

  /** 属性类型 */
  private String propertyType;

  private String propertySimpleType;

  public void setName(String name) {
    this.name = name;
    this.property = buildPropertyName();
  }

  public void setPropertyType(String propertyType) {
    this.propertyType = propertyType;
    this.propertySimpleType = propertyType.substring(propertyType.lastIndexOf(".") + 1);
  }

  public void setComment(String comment) {
    this.comment = StrUtil.replace(comment, "\n", "");
  }

  public Boolean getAutoIncrement() {
    return autoIncrement;
  }

  public void setAutoIncrement(Boolean autoIncrement) {
    this.autoIncrement = autoIncrement;
  }

  public String buildPropertyName() {
    String entityJavaFileName = name;
    return StrUtil.lowerFirst(StrUtil.toCamelCase(entityJavaFileName));
  }

  @Override
  public String toString() {
    return "Column{"
        + "name='"
        + name
        + '\''
        + ", className='"
        + propertyType
        + '\''
        + ", remarks='"
        + comment
        + '\''
        + ", isAutoIncrement="
        + autoIncrement
        + '}';
  }
}
